1 # Colin McNicholl
2 # 18/04/2019
3
4 import random
5 HANGMAN_PICS = ['''
6 +---+
7 |
8 |
9 |
10 ===''', '''
11 +---+
12 O |
13 |
14 |
15 ===''', '''
16 +---+
17 O |
18 | |
19 |
20 ===''', '''
21 +---+
22 O |
23 /| |
24 |
25 ===''', '''
26 +---+
27 O |
28 /|\ |
29 |
30 ===''', '''
31 +---+
32 O |
33 /|\ |
34 / |
35 ===''', '''
36 +---+
37 O |
38 /|\ |
39 / \
40 ===''', '''
41 +---+
42 [O |
43 /|\ |
44 / \ |
45 ===''', '''
46 [O] |
47 /|\ |
48 / \ |
49 ===''']
50 words = {'Colors': 'red orange yellow green blue indigo violet white black brown'.split(),
51 'Shapes': 'square triangle rectangle circle ellipse rhombus trapezoid chevron pentagon hexagon septagon octagon'.split(),
52 'Fruits': 'apple orange lemon lime pear watermelon grape grapefruit cherry banana canteloupe mango strawberry tomato'.split(),
53 'Animals': 'bat bear beaver cat cougar crab deer dog donkey duck eagle fish frog goat leech lion lizard monkey moose mouse otter owl panda python rabbit rat shark sheep skunk squid tiger turkey turtle weasel whale wolf wombat zebra'.split()}
54
55 def getRandomWord(wordDict):
56 '''This function returns a random string from the passed dictionary of
57 lists of strings and its key.'''
58 # First, randomely select a word from the key's list in the dictionary:
59 wordKey = random.choice(list(wordDict.keys()))
60
61 # Second, randomely select a word from the key's list in the dictionary:
62 wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)
63
64 return [wordDict[wordKey][wordIndex], wordKey]
65
66 def displayBoard(missedLetters, correctLetters, secretWord):
67 print(HANGMAN_PICS[len(missedLetters)])
68 print()
69
70 print('Missed letters:', end=' ')
71 for letter in missedLetters:
72 print(letter, end=' ')
73 print()
74
75 blanks = '_' * len(secretWord)
76
77 for i in range(len(secretWord)): # Replace blanks with correctly guessed letters
78 if secretWord[i] in correctLetters:
79 blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
80
81 for letter in blanks: # Show the secret word with spaces in between each letter
82 print(letter, end=' ')
83 print()
84
85 def getGuess(alreadyGuessed):
86 ''' Returns the letter the player entered. This function makes sure
87 the player entered a single letter and not something else.'''
88 while True:
89 print('Guess a letter.')
90 guess = input()
91 guess = guess.lower()
92 if len(guess) != 1:
93 print('Please enter a single letter.')
94 elif guess in alreadyGuessed:
95 print('You have already guessed that letter. Choose again.')
96 elif guess not in 'abcdefghijklmnopqrstuvwxyz':
97 print('Please enter a LETTER.')
98 else:
99 return guess
100
101 def playAgain():
102 '''This function returns True if the player wants to play again;
103 otherwise, it returns False.'''
104 print('Do you want to play again? (yes or no)')
105 return input().lower().startswith('y')
106
107
108 print('H A N G M A N')
109
110 difficulty = 'X'
111 while difficulty not in ['E', 'M', 'H']:
112 print('Enter difficulty: E - Easy, M - Medium, H - Hard')
113 difficulty = input().upper()
114 if difficulty == 'M':
115 del HANGMAN_PICS[8]
116 del HANGMAN_PICS[7]
117 if difficulty == 'H':
118 del HANGMAN_PICS[8]
119 del HANGMAN_PICS[7]
120 del HANGMAN_PICS[5]
121 del HANGMAN_PICS[3]
122
123 missedLetters = ''
124 correctLetters = ''
125 secretWord, secretSet = getRandomWord(words)
126 gameIsDone = False
127
128 while True:
129 print('The secret word is in the set: ' + secretSet)
130 displayBoard(missedLetters, correctLetters, secretWord)
131
132 # Let the player enter a letter
133 guess = getGuess(missedLetters + correctLetters)
134
135 if guess in secretWord:
136 correctLetters = correctLetters + guess
137
138 # Check if the player has won
139 foundAllLetters = True
140 for i in range(len(secretWord)):
141 if secretWord[i] not in correctLetters:
142 foundAllLetters = False
143 break
144 if foundAllLetters:
145 print('Yes! The secret word is "' + secretWord + '"! You have won!')
146 gameIsDone = True
147 else:
148 missedLetters = missedLetters + guess
149
150 # Check if the player has guessed too many times and lost
151 if len(missedLetters) == len(HANGMAN_PICS) - 1:
152 displayBoard(missedLetters, correctLetters, secretWord)
153 print('You have run out of guesses!\nAfter ' +
154 str(len(missedLetters)) + ' missed guesses and ' +
155 str(len(correctLetters)) + ' correct guesses,' +
156 ' the word was "' + secretWord + '"')
157 gameIsDone = True
158
159 '''Ask the player if they want to play again (but only if the game is
160 done).'''
161 if gameIsDone:
162 if playAgain():
163 missedLetters = ''
164 correctLetters = ''
165 gameIsDone = False
166 secretWord, secretSet = getRandomWord(words)
167 else:
168 break